<?php
/***************************************************************************
 * @Description:
 * 
 * @FileName:         ContentController.php
 * @Author :          liyong5@staff.sina.com.cn
 * @CreatedTime:      Mon 28 May 2018 05:22:39 PM CST
 ************************************************************************/

namespace app\modules\core\controllers;

use Yii;
use app\modules\core\models\Field;
use app\modules\core\models\Content;
use app\modules\core\models\Model;
use yii\mongodb\Query;
use yii\data\Pagination;

class ContentController extends DefaultController
{
    public function actions() {
        return array_merge(parent::actions(), []);
    }

    public function actionList() {
        $query = new Query();

        $query->from('content');

        $pagination = new Pagination([
            'defaultPageSize' => 20,
            'totalCount' => $query->count(),
        ]);

        $contents = $query->offset($pagination->offset)
            ->limit($pagination->limit)
            ->all();

        return $this->render('list', ['contents' => $contents,
            'pagination' => $pagination]);
    }

    public function actionCreate() {
        $request = Yii::$app->request;

        $model = Model::find()
            ->where(['id' => $request->get('id')])
            ->limit(1)
            ->one();

        if (! $model) {
            return $this->redirect(['/core/content/list']);
        }

        $model->fillFields();

        // 通过model对象，动态设置Content对像的属性
        $content = new Content();
        $content->setAttrs($model->fieldAttrs);
        $content->setRules($model->genRules());
        $content->setAttrLable($model->genLables());

        //
        if (! $content->load($request->post(), 'Content')) {
            return $this->render('edit', ['model' => $model, 'content' => $content]);
        }

        $content->id = $content->genId();
        $content->model = $model->id;
        $content->ctime = time();
        if ($content->validate() && $content->save()) {
            return $this->redirect(['/core/content/list']);
        } else {
            return $this->render('edit', ['model' => $model, 'content' => $content]);
        }
    }

    public function actionEdit() {
        $request = Yii::$app->request;

        $query = new Query();

        $data = $query->from('content')
            ->where(['id' => $request->get('id')])
            ->limit(1)
            ->one();

        $model = Model::find()
            ->where(['id' => $data['model']])
            ->limit(1)
            ->one();

        $model->fillFields();

        $content = new Content();
        $content->setAttrs($model->fieldAttrs);
        $content->setRules($model->genRules());
        $content->setAttrLable($model->genLables());
        
        $content->load($data, '');
        $content->setIsNewRecord(false);

        //
        if (null === $request->post('id')) {
            return $this->render('edit', ['content' => $content, 'model' => $model]);
        }
        if (! $content->load($request->post())) {
            return $this->render('edit', ['content' => $content, 'model' => $model]);
        }

        $content->ctime = time();
        if ($content->validate()
                && $content->save()) {
            return $this->redirect(['/core/content/list']);
        }
        return $this->render('edit', ['content' => $content, 'model' => $model]);
    }

    public function actionDelete() {
        $request = Yii::$app->request;

        if (! ($contentId = $request->get('id'))) {
            return $this->asJson(['error' => 'content id error']);
        }

        if (Content::findOne($contentId)->delete()) {
            return $this->asJson(['error' => 'fail']);
        }
        return $this->asJson(['msg' => 'success']);
    }
}
